﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Media.Animation;
using System.Diagnostics;
using EventHub.Data.DataAccess;
using EventHub.UI.ViewModel;

namespace EventHub.UI
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Default : Window
    {
        private BackgroundWorker _treeViewWorker = new BackgroundWorker();
        private BackgroundWorker _eventHierarchyWorker = new BackgroundWorker();

        EventHubModelDataAdapter _da = new EventHubModelDataAdapter();
        
        public Default()
        {            
            InitializeComponent();
            InitializeTreeViewWorker();
            InitializeControls();            
        }

        private void InitializeTreeViewWorker()
        {
            _treeViewWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(UpdateEventsCompleted);
            _treeViewWorker.DoWork += new DoWorkEventHandler(UpdateEvents);            
            _treeViewWorker.WorkerSupportsCancellation = true;

            _eventHierarchyWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(EventHierarchyPopulated);
            _eventHierarchyWorker.DoWork += new DoWorkEventHandler(PopulateEventHierarchy);
            _eventHierarchyWorker.WorkerSupportsCancellation = true;
        }

        void PopulateEventHierarchy(object sender, DoWorkEventArgs e)
        {            
            var selectedEvent = e.Argument.ToString();
            var rootEvent = _da.LoadEventTree(selectedEvent);    
            EventTreeViewModel viewModel = new EventTreeViewModel(rootEvent);
            
            EventHierarchyTree.Dispatcher.BeginInvoke(new Action(
                delegate()
                {
                    EventHierarchyTree.DataContext = viewModel;
                }
            ));
            viewModel.ContextEvent = selectedEvent;
            viewModel.SearchCommand.Execute(null);
        }

        void EventHierarchyPopulated(object sender, RunWorkerCompletedEventArgs e)
        {
            EventHierarchyLoadCancel.Visibility = Visibility.Collapsed;
            EventHorizontalProgressIndicator.Visibility = Visibility.Collapsed;
        }

        private void InitializeControls()
        {            
            UpdateProgressImage.Visibility = Visibility.Collapsed;
            EventHierarchyLoadCancel.Visibility = Visibility.Collapsed;
            EventHorizontalProgressIndicator.Visibility = Visibility.Collapsed;
            this.Width = 800; 
            this.Height = 600;
        }        

        void UpdateEventsCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            UpdateProgressImage.Visibility = Visibility.Collapsed;
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!_treeViewWorker.IsBusy)
            {
                UpdateProgressImage.Visibility = Visibility.Visible;
                _treeViewWorker.RunWorkerAsync(EventText.Text);
            }
        }

        void UpdateEvents(object sender, DoWorkEventArgs e)
        {            
            string[] matchingEvents = _da.GetMatchingEvents(e.Argument.ToString());            
            EventTree.Dispatcher.BeginInvoke(new Action(
                delegate()
                {
                    EventTree.Items.Clear();
                    foreach (var item in matchingEvents)
                    {
                        EventTree.Items.Add(item);
                    }
                }
            ));
        }

        private void UpdateProgressImage_Click(object sender, RoutedEventArgs e)
        {
            if (!_treeViewWorker.CancellationPending)
            {
                _treeViewWorker.CancelAsync();
            }
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
        }

        private void EventText_MouseLeave(object sender, MouseEventArgs e)
        {
            ((TextBox)sender).BeginAnimation(TextBox.EffectProperty, null);
        }

        private void EventTree_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!_eventHierarchyWorker.IsBusy && EventTree.SelectedItem != null)
            {                
                EventHierarchyLoadCancel.Visibility = Visibility.Visible;
                EventHorizontalProgressIndicator.Visibility = Visibility.Visible;
                _eventHierarchyWorker.RunWorkerAsync(EventTree.SelectedItem);
            }
        }
        
        private void EventHierarchyLoadCancel_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
